﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using Tools;

namespace Punia
{
    [CommandAttribute(true)]
    public partial class receive : BaseForm
    {
        public receive()
            : base()
        {
        }

        public receive(DbContext dbContext)
            : base(dbContext)
        {
            InitializeComponent();

            cbQueue.DataSource = Utility.queues(dbContext);
            cbQueue.DisplayMember = "Name";

        }

        protected override void Write()
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("USE [" + dbContext.Database + "];");
            sb.AppendLine();
            sb.AppendLine("DECLARE @reply NVARCHAR(MAX);");
            sb.AppendLine("DECLARE @sep NVARCHAR(10);");
            sb.AppendLine("DECLARE @ch UNIQUEIDENTIFIER");
            sb.AppendLine("DECLARE @messagetypename NVARCHAR(256)");
            sb.AppendLine("DECLARE @messagebody NVARCHAR(MAX);");
            sb.AppendLine("SET @reply = NULL; ");
            sb.AppendLine("SET @sep = N'" + mainForm.sep + "';");
            sb.AppendLine("BEGIN TRY");
            sb.AppendLine("   BEGIN TRANSACTION");
            sb.AppendLine();
            sb.AppendLine("      WAITFOR (");
            sb.AppendLine("         RECEIVE TOP(1)");
            sb.AppendLine("            @ch = conversation_handle, ");
            sb.AppendLine("            @messagetypename = message_type_name, ");
            sb.AppendLine("            @messagebody = CAST(message_body AS NVARCHAR(MAX))");

            if (cbQueue.SelectedIndex > 0)
                sb.AppendLine("         FROM " + cbQueue.Text);
            else
                sb.AppendLine("         FROM []");

            sb.AppendLine("      ), TIMEOUT " + tbTimeout.Text + ";");
            sb.AppendLine("      IF (@@ROWCOUNT > 0)");
            sb.AppendLine("      BEGIN");
            sb.AppendLine("         SET @reply = CAST(@ch AS NVARCHAR(256))+@sep+ISNULL(@messagetypename,'')+@sep+ISNULL(@messagebody,'');");
            if (cbEnd.Checked)
                sb.AppendLine("     END CONVERSATION @ch;");
            sb.AppendLine("      END;");
            sb.AppendLine("   COMMIT TRANSACTION");
            sb.AppendLine("END TRY");
            sb.AppendLine("BEGIN CATCH");
            sb.AppendLine("   ROLLBACK TRANSACTION;");
            sb.AppendLine("   DECLARE @m nvarchar(max)");
            sb.AppendLine("   SET @reply = 'error';");
            sb.AppendLine("   SET @m = error_message()");
            sb.AppendLine("   RAISERROR('%s',16,1,@m)");
            sb.AppendLine("END CATCH;");
            sb.AppendLine("IF (@reply IS NOT NULL)");
            sb.AppendLine("BEGIN");
            sb.AppendLine("   SELECT @messagetypename As [Message type], CAST(@messagebody as xml) AS [Message body]; "); 
            sb.AppendLine("   PRINT 'Message type: '+ISNULL(@messagetypename,'');");
            sb.AppendLine("   PRINT 'Message body: '+ISNULL(@messagebody,'');");
            sb.AppendLine("END");

            tbCommand.Text = sb.ToString();
        }

        private void cbQueue_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbTimeout_TextChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbMessType_SelectedIndexChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void cbEnd_CheckedChanged(object sender, EventArgs e)
        {
            Write();
        }

        private void tbSend_TextChanged(object sender, EventArgs e)
        {
            Write();
        }
    }
}